package com.xyht.sca_s.student_manage_system.modules.welcomeNewStudent.util;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xyht.sca_s.student_manage_system.modules.org.entity.SmsOrgStructure;
import com.xyht.sca_s.student_manage_system.modules.org.entity.SmsOrgUserRelation;
import com.xyht.sca_s.student_manage_system.modules.org.mapper.SmsOrgStructureMapper;
import com.xyht.sca_s.student_manage_system.modules.org.mapper.SmsOrgUserRelationMapper;
import com.xyht.sca_s.student_manage_system.modules.student.entity.SmsUserStudentRelation;
import com.xyht.sca_s.student_manage_system.modules.student.mapper.SmsUserStudentRelationMapper;
import com.xyht.sca_s.student_manage_system.modules.user.entity.SmsRole;
import com.xyht.sca_s.student_manage_system.modules.user.entity.SmsUser;
import com.xyht.sca_s.student_manage_system.modules.user.entity.SmsUserRoleRelation;
import com.xyht.sca_s.student_manage_system.modules.user.mapper.SmsRoleMapper;
import com.xyht.sca_s.student_manage_system.modules.user.mapper.SmsUserMapper;
import com.xyht.sca_s.student_manage_system.modules.user.mapper.SmsUserRoleRelationMapper;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

import static com.xyht.sca_s.student_manage_system.common.util.ParamCheckUtil.isNullOrEmpty;
import static com.xyht.sca_s.student_manage_system.modules.dormitory.constant.OrgLevelConstant.CLASS_LEVEL;
import static com.xyht.sca_s.student_manage_system.modules.welcomeNewStudent.constant.RoleNameConstant.ROLE_NAME_ASSISTANT;

/**
 * 辅导员
 */
@Component
public class AssistantUtil {

    @Resource
    private SmsUserRoleRelationMapper smsUserRoleRelationMapper;
    @Resource
    private SmsRoleMapper smsRoleMapper;
    @Resource
    private SmsUserMapper smsUserMapper;
    @Resource
    private SmsOrgUserRelationMapper smsOrgUserRelationMapper;
    @Resource
    private SmsOrgStructureMapper smsOrgStructureMapper;
    @Resource
    private SmsUserStudentRelationMapper smsUserStudentRelationMapper;

    /**
     * @param classOrgId  班级组织id
     * @return 该班的辅导员用户信息
     */
    public List<SmsUser> getAssistantUserInfo(String classOrgId) {

        //先查询该班级id关联的用户id
        List<String> orgUserIdList = smsOrgUserRelationMapper.selectList(new LambdaQueryWrapper<SmsOrgUserRelation>()
                .eq(SmsOrgUserRelation::getOrgId, classOrgId))
                .stream().map(SmsOrgUserRelation::getUserId).collect(Collectors.toList());
        //查询用户中是教师的用户id
       LambdaQueryWrapper<SmsUser> userLambdaQueryWrapper =  new LambdaQueryWrapper<SmsUser>()
                .eq(SmsUser::getFlag, 0);
        if (!orgUserIdList.isEmpty()) {
            userLambdaQueryWrapper.in(SmsUser::getId,orgUserIdList);
        }else {
            userLambdaQueryWrapper.in(SmsUser::getId,"");
        }
        List<SmsUser> tchUserList = smsUserMapper.selectList(userLambdaQueryWrapper);
        //过滤出教师中是辅导员角色的用户id
        List<SmsUser> assistantUserList = tchUserList.stream().filter(tchUser -> {
            List<String> roleIdList = smsUserRoleRelationMapper.selectList(new LambdaQueryWrapper<SmsUserRoleRelation>()
                    .eq(SmsUserRoleRelation::getUserId, tchUser.getId()))
                    .stream()
                    .map(SmsUserRoleRelation::getRoleId).collect(Collectors.toList());
            if (roleIdList.isEmpty()) {
                return false;
            }
            //角色是辅导员
            List<SmsRole> roleFilterList = smsRoleMapper.selectList(new LambdaQueryWrapper<SmsRole>()
                    .in(SmsRole::getId, roleIdList)
                    .eq(SmsRole::getRoleName, ROLE_NAME_ASSISTANT));
            if (roleFilterList.isEmpty()) {
                return false;
            }
            return true;
        }).collect(Collectors.toList());
        return assistantUserList;
    }

    /**
     *
     * @param userId 学生用户id
     * @return 返回该学生的辅导员信息
     */
    public List<SmsUser> getAssistantByUserId(String userId) {
        //获取用户的组织
        List<String> orgIdList = smsOrgUserRelationMapper.selectList(new LambdaQueryWrapper<SmsOrgUserRelation>()
                .eq(SmsOrgUserRelation::getUserId, userId))
                .stream().map(SmsOrgUserRelation::getOrgId).collect(Collectors.toList());
        if (orgIdList.isEmpty()) {
//            ExceptionCast.cast(CommonResult.failed(CommonCodeEnum.STUDENT_NOT_EXIST));
            return null;
        }
        SmsOrgStructure smsOrgStructure = smsOrgStructureMapper.selectOne(new LambdaQueryWrapper<SmsOrgStructure>()
                .eq(SmsOrgStructure::getLevel, CLASS_LEVEL)
                .in(SmsOrgStructure::getId, orgIdList));
        if (isNullOrEmpty(smsOrgStructure)) {
            return null;
        }
        return getAssistantUserInfo(String.valueOf( smsOrgStructure.getId()));
    }

    /**
     * 根据stuId获取辅导员信息集合
     *
     * @param stuId stuId
     * @return 辅导员信息集合
     */
    public List<SmsUser> getAssistantsByStuId(String stuId) {
        List<SmsUserStudentRelation> userStudentRelationList = smsUserStudentRelationMapper.selectList(new LambdaQueryWrapper<SmsUserStudentRelation>()
                .eq(SmsUserStudentRelation::getStuId, stuId));
        if (userStudentRelationList.isEmpty()) {
            return null;
        }

        return getAssistantByUserId(userStudentRelationList.get(0).getUserId());
    }

}
